/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.Order.Order;
import DTO.Order.OrderDetail;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.DBUtil;

/**
 *
 * @author ThanhTai
 */
public class OrderDAO {

    public boolean InsertOrder(Order order) {
        DBUtil util = new DBUtil();
        Connection conn = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            Class.forName(util.getDriverName());
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            conn = DriverManager.getConnection(util.getURL(), util.getUsername(), util.getPassword());
            conn.setAutoCommit(false);
            Integer countStm = 0;
            String sql = "";
            if (conn != null) {
                countStm++;
                sql = "Insert into [Order](custEmail, description, total, date, status) values (?, ?, ?, ?, ?)";
                stm = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                stm.setString(1, order.getCustEmail());
                stm.setString(2, order.getDescription());
                stm.setLong(3, order.getTotal());
                stm.setLong(4, order.getDate());
                stm.setString(5, order.getStatus());
                stm.executeUpdate();
                rs = stm.getGeneratedKeys();
                Integer orderID = -1;
                if (rs.next()) {
                    orderID = rs.getInt(1);
                }

                if (orderID != -1) {
                    order.setOrderID(orderID);
                    List<OrderDetail> orderDetails = order.getOrderDetails();
                    for (int i = 0; i < orderDetails.size(); i++) {
                        OrderDetail orderDetail = orderDetails.get(i);
                        sql = "Insert into [OrderDetail](orderID, productID, productName, productPrice, quantity) values (?, ?, ?, ?, ?)";
                        stm = conn.prepareStatement(sql);
                        stm.setInt(1, orderID);
                        stm.setInt(2, orderDetail.getProductID());
                        stm.setString(3, orderDetail.getProductName());
                        stm.setLong(4, orderDetail.getProductPrice());
                        stm.setInt(5, orderDetail.getQuantity());
                        stm.executeUpdate();
                    }
                }
                conn.commit();
                return true;
            }
            return false;
        } catch (SQLException ex) {
            ex.printStackTrace();
            try {
                if (conn != null) {
                    conn.rollback();
                    return false;
                }
                return false;
            } catch (SQLException ex2) {
                System.out.println("Caught SQL Exception: " + ex2);
                return false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                conn.rollback();
                return false;
            } catch (SQLException ex1) {
                Logger.getLogger(OrderDAO.class.getName()).log(Level.SEVERE, null, ex1);
                return false;
            }
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
    }
}
